import os
from pysqlcipher3 import dbapi2 as sqlite3

# 数据库文件路径
DB_PATH = os.path.join(os.path.dirname(__file__), "../sql/uni-video.db")
SCHEMA_PATH = os.path.join(os.path.dirname(__file__), "../sql/init-schema.sql")
DATA_PATH = os.path.join(os.path.dirname(__file__), "../sql/init-data.sql")
DB_PASSWORD = "root123"  # 数据库加密密码

# 如果数据库已存在则删除
if os.path.exists(DB_PATH):
    os.remove(DB_PATH)

# 连接加密数据库
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute(f"PRAGMA key='{DB_PASSWORD}';")

# 执行 schema 文件
with open(SCHEMA_PATH, "r", encoding="utf-8") as f:
    schema_sql = f.read()
    cursor.executescript(schema_sql)

# 执行 data 文件
with open(DATA_PATH, "r", encoding="utf-8") as f:
    data_sql = f.read()
    cursor.executescript(data_sql)

# 插入 root 用户
cursor.execute(
    """
    INSERT INTO uni_user (
        username, password, douyin_id, avatar, signature, school, phone, city, birthday, gender, email, fans_count, follow_count, like_count, comment_count
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
    (
        "root",
        "root123",
        "dy0000",
        "/static/person/root.jpg",
        "管理员",
        "系统",
        "00000000000",
        "系统",
        "1970-01-01",
        0,
        "root@admin.com",
        0,
        0,
        0,
        0,
    ),
)

conn.commit()
cursor.close()
conn.close()
print("加密数据库初始化完成，root用户已创建。")
